package com.turman.blibrary.util;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import java.util.ArrayDeque;
import java.util.Deque;

/**
* view utils
* @author buobao
* @since 2024/3/12 8:48
*/
public class BViewUtil {

    /**
    * 查询指定类型的view
    * @author buobao
    * @since 2024/3/11 15:26
    */
    public static <T> T findTypeView(@Nullable ViewGroup viewGroup,Class<T> cls) {
        if (viewGroup == null) {
            return null;
        }

        Deque<View> deque = new ArrayDeque<>();
        deque.add(viewGroup);
        while (!deque.isEmpty()) {
            View view = deque.removeFirst();
            if (cls.isInstance(view)) {
                return cls.cast(view);
            } else if (view instanceof ViewGroup) {
                ViewGroup container = (ViewGroup) view;
                for (int i=0,count = container.getChildCount();i<count;i++) {
                    deque.add(container.getChildAt(i));
                }
            }
        }
        return null;
    }
}
